/**
 * 
 * @author Adam Till
 *
 */

package com.deltarail.snmp.spy.main;

import java.net.URL;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * 
 * The main class, loading the application
 *
 */

public class Main extends Application
{
	/** Initial and minimal scene/stage width. */	
	public final static int DEFAULT_WIDTH = 800;

	/** Initial and minimal scene/stage height. */
	public final static int DEFAULT_HEIGHT = 800;

	@Override
	/**
	 * Starts the application
	 */

	public void start (final Stage primaryStage)
	{
		try

		{
			// Load the main window
			final URL resource = getClass().getResource( "MainWindow.fxml");
			System.out.println(resource.toString());
			final FXMLLoader loader = new FXMLLoader(resource);
			
			// Get the associated pane
			AnchorPane pane = (AnchorPane) loader.load();
			
			// Set scene width, height and style
			final Scene scene = new Scene(pane, DEFAULT_WIDTH, DEFAULT_HEIGHT);			
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			
			// Set the stage's properties
			primaryStage.setScene(scene);
			primaryStage.setMinHeight(DEFAULT_HEIGHT);
			primaryStage.setMinWidth(DEFAULT_WIDTH);
			
			// Show the main window
			primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
				
				@Override
				public void handle(WindowEvent event) 
				{
					((UIController) loader.getController()).setExitNow(true);
					
				}
			});
			primaryStage.show();
			
		}
		
		catch (Exception e)
		{
			System.out.println("Error while loading the main window");
			
			e.printStackTrace();
		}
	}
	public static void main(String[] args)
	{
		launch(args);
	}
}
